package com.ysy.logistics.entities.system;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ysy.logistics.entities.system.vo.MenuVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/3/19 16:46
 **/
@Data
@TableName("sys_menu")
@Schema(name = "sys_menu", description = "菜单节点")
public class MenuNode {

    @TableId(value = "id", type = IdType.AUTO)
    @Schema(description = "节点id")
    private Long id;


    @Schema(description = "父节点id")
    @TableField(value = "parent_id")
    private Long parentId;

    @Schema(description = "父菜单编码")
    @TableField(value = "pcode")
    private String pcode;


    @Schema(description = "节点名称")
    @TableField(value = "name")
    private String name;

    @Schema(description = "节点路径")
    @TableField(value = "path")
    private String url;

    /**
     * 节点图标
     */
    @Schema(description = "节点图标")
    @TableField(value = "icon")
    private String icon;
    /**
     * 菜单编码
     */
    @Schema(description = "菜单编码")
    @TableField(value = "code")
    private String code;
    /**
     * 組件配置
     */
    @Schema(description = "組件配置")
    @TableField(value = "component")
    private String component;
    /**
     * 子节点的集合
     */
    @Schema(description = "子节点的集合")
    @TableField(exist = false)
    private List<MenuNode> children;


    @Schema(description = "是否隐藏")
    @TableField(value = "hidden")
    private Boolean hidden;

    @Schema(description = "菜单权重")
    @TableField(value = "num")
    private Integer num;

    @Schema(description = "是否菜单")
    @TableField(value = "is_menu")
    private Integer isMenu;


    public static List<MenuVO> buildTree(List<MenuNode> nodes) {
        List<MenuVO> tree = new ArrayList<>();
        for (MenuNode node : nodes) {
            MenuVO menuVO = MenuVO.builder()
                    .id(node.getId().intValue())
                    .name(node.getCode())
                    .path(node.getUrl())
                    .component(node.getComponent())
                    .parentId(node.getParentId().intValue())
                    .meta(Meta.builder()
                            .title(node.getName())
                            .icon(node.getIcon())
                            .build())
                    .children(buildTree(node.getChildren()))
                    .build();
            tree.add(menuVO);
        }
        return tree;
    }
}
